package demo.string;

public class MinDistance {

    public static void main(String[] args) {
        String[] strArr = "3313333231".split("");
        System.out.println(minDistance(strArr, "1", "2"));
    }

    public static int minDistance(String[] strArr, String s1, String s2) {
        if (s1 == null || s2 == null) {
            return -1;
        }

        if (s1.equals(s2)) {
            return 0;
        }

        int last1 = -1;
        int last2 = -1;
        int min = Integer.MAX_VALUE;
        for (int i = 0; i < strArr.length; i++) {
            if (strArr[i].equals(s1)) {
                min = Math.min(min, last2 == -1 ? min : i - last2);
                last1 = i;
            }

            if (strArr[i].equals(s2)) {
                min = Math.min(min, last1 == -1 ? min : i - last1);
                last2 = i;
            }
        }
        return min == Integer.MIN_VALUE ? -1 : min;
    }
}
